package cn.endeavour.order.controller;

import cn.endeavour.basic.util.AjaxResult;
import cn.endeavour.basic.util.PageList;
import cn.endeavour.order.domain.AdoptOrder;
import cn.endeavour.order.query.AdoptOrderQuery;
import cn.endeavour.order.service.IAdoptOrderService;
import cn.endeavour.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.security.auth.login.LoginContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("adoptOrder")
public class AdoptOrderController {

    @Autowired
    private IAdoptOrderService adoptOrderService;

    /**
     * 管理员：管理员查询订单接口 - 平台管理员所有店铺的所有订单，店铺管理员查询当前店铺的所有订单
     * @param adoptOrderQuery 订单查询参数
     * @param request
     * @return
     */
    @PostMapping("/admin")
    public PageList<AdoptOrder> adminAdoptOrder(@RequestBody AdoptOrderQuery adoptOrderQuery, HttpServletRequest request){
        return adoptOrderService.queryAdminOrder(adoptOrderQuery, request);
    }


    /**
     * 用户：查询订单接口 - 只能查询自己的订单
     * @param adoptOrderQuery 订单查询参数
     * @param request
     * @return
     */
    @PostMapping("/user")
    public PageList<AdoptOrder> userAdoptOrder(@RequestBody AdoptOrderQuery adoptOrderQuery, HttpServletRequest request){
        return adoptOrderService.queryUserOrder(adoptOrderQuery, request);
    }

    /**
     * 宠物领养 + 创建订单
     * @param params
     * @param request
     * @return
     */
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String,Object> params, HttpServletRequest request){
        try {
            String payData = adoptOrderService.adoptOrderSubmit(params, request);
            return AjaxResult.me().setSuccess(true).setResultObj(payData);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("领养失败!" + e.getMessage());
        }
    }
}
